package com.rong.action;
 
 import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionSupport;
 import com.rong.DAO.ZynUserDAO;
 import com.rong.DAO.ZynParaDAO;
 import com.rong.ORM.ZynPara;
 import com.rong.ORM.ZynUser;
 import com.rong.ORM.ZynCompany;
import com.rong.ORM.ZynPrice1;
import com.rong.action.RegisterAction;
import com.rong.DAO.ZynUserRightDAO;
import com.rong.DAO.ZynMenuDAO;
import com.rong.action.ComCarEtcListAction;
import com.rong.hibernate.BaseHibernateDAO;
import com.rong.hibernate.Const;
import com.rong.ORM.ZynMenu;
import com.rong.ORM.ZynUserRight;
 import com.rong.DAO.ZynCompanyDAO;
 import com.rong.DAO.ZynCarDAO;
 import com.rong.DAO.ZynStatusDAO;
 
public class LoginAction extends ActionSupport {
    
    
    private ZynUserDAO userDao = new ZynUserDAO();    //
    private ZynParaDAO parameterDao = new ZynParaDAO();    //
    private ZynUserRightDAO urDao=new ZynUserRightDAO();
    ComCarEtcListAction getListAction=new ComCarEtcListAction();
    ZynMenuDAO menuDao=new ZynMenuDAO();
    BaseHibernateDAO b=new BaseHibernateDAO();
    private ZynCompanyDAO comDao= new ZynCompanyDAO();
    ZynCarDAO carDao =new ZynCarDAO();
    ZynStatusDAO statusDao=new ZynStatusDAO();
     
    private int id;
    private String name;    //
    private String pwd;        //
    
    
    
   // private String inputCertCode;    //

    public int getId() {
        return id;
   }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String execute() throws Exception {
        int state = userDao.isExist(name,pwd);
        System.out.println("state="+state);
        if(state == -1){
            this.addFieldError("name", "用户名错误!");
            return INPUT;
        }else if(state == -2){
            this.addFieldError("pwd", "密码错误!");
            return INPUT;
        }else if(state > 0){
            System.out.println("[LoginAction]ID="+state);
            this.setId(state);
            
             
            
            //-------------get the comcpany list and the car list,put them into the session .wafech 2010-12-09
            getListAction.getList();
            ActionContext actionContext = ActionContext.getContext();         
            Map session = actionContext.getSession(); 
            ZynUser loguser=userDao.findById(state);
            session.put("loginUser",loguser);    // the user session
            session.put("status_list", statusDao.findAll());
            if(loguser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){//公司本部可以查看所有部门，车，用户
		            
		            session.put("company_list", getListAction.getComlist());         
		            session.put("car_list", getListAction.getCarlist());   
		            session.put("user_list",userDao.findAll());
		            		         
		            session.put("company_list_suply", comDao.findByComProperty(Const.comTypesupply));//供应商列表
		            session.put("company_list_custom", comDao.findByComProperty(Const.comTypecustom));  //客户列表
		            List topsublist=comDao.findByComProperty(Const.comTypetop);
		            List sublist=comDao.findByComProperty(Const.comTypesubcom);
		            for(int m=0;m<sublist.size();m++){
		            	topsublist.add(sublist.get(m));
		            }
		            
		            session.put("subcom_list",topsublist);  //所有分公司列表+公司本部		            
            }else{																	//分公司人员进入限制查看其它分公司情况
                
	            session.put("company_list", comDao.findByExtAttr3(loguser.getComId().toString()));  
	            session.put("company_list_suply", comDao.findProptAndBlto(Const.comTypesupply, loguser.getComId().toString()));//供应商列表
	            session.put("company_list_custom", comDao.findProptAndBlto(Const.comTypecustom,loguser.getComId().toString()));  //客户列表

	            session.put("subcom_list",comDao.findById(loguser.getZyncom().getId()));  //分公司列表
	            
	            //	            
//	            System.out.println("sup com:"+loguser.getComId().toString());
//	            List<ZynCompany> clist =comDao.findProptAndBlto(Const.comTypesupply, loguser.getComId().toString());
//	            for(int p=0;p<clist.size();p++){
//	            	ZynCompany t=clist.get(p);
//	            	System.out.println("==供应商=="+t.getComName());
//	            }
//	            
//	            
	            session.put("car_list", carDao.findByCompany(loguser.getComId().toString()));   
	            session.put("user_list",userDao.findByComId(loguser.getComId()));      	
            }
            
            session.put("menu_list",menuDao.findAll());
            session.put("mymenu_list",urDao.findByUserId(state));
           
            //-------------
            
          //-------------statmStyle 
            TreeMap<String, String> map=new TreeMap();
            map.put(Const.statmStyleCAR,"按车结算");
            map.put(Const.statmStyleTON,"按吨结算");          
            map.put(Const.statmStyleA,"按热量表结算");
            map.put(Const.statmStyleCUBE,"按立方结算");
            map.put(Const.statmStyleB,"结算方式B");
            map.put(Const.statmStyleC,"结算方式C");
            session.put("statmap",map);  
          //-------------statmStyle 
            
            //-------------the type of Steam 
            TreeMap<String, String> mapsteam=new TreeMap();
            mapsteam.put(Const.exhSteam,"废蒸汽");
            mapsteam.put(Const.direcSteam,"直接蒸汽");
            mapsteam.put(Const.otherSteam,"其它类型蒸汽");
            session.put("map_steam",mapsteam);  
            
            //-------------the type of give heat 
            TreeMap<String, String> mapheatsytle=new TreeMap();
            mapheatsytle.put(Const.heatWater,"供水");
            mapheatsytle.put(Const.heatSteam,"供热");
            mapheatsytle.put(Const.heatOther,"其它");
            session.put("map_heat_style",mapheatsytle);    
                
         //-----set the para or 充热温度
               //--1--    
            Session s=null;
            s=b.getSession();
            Criteria c_para=s.createCriteria(ZynPara.class);
	         c_para.add(Restrictions.and(Restrictions.isNotNull("extparaDb1"), Restrictions.or(Restrictions.isNull("invalidationDate"),Restrictions.gt("invalidationDate", new Date())))); //日期为空或大于当下日期      
	         List<ZynPara> list=c_para.list();			        	
		        	if(list.size()>0){
		        		 session.put("upTemprature", list.get(0).getExtparaDb1());            
				        
		        	}else{//没有对应记录，返回0		        		
		        		session.put("upTemprature", 0);  
		        	}                          
		     
		        //--2--
		        	Criteria c_oil=s.createCriteria(ZynPara.class);
		        	c_oil.add(Restrictions.and(Restrictions.isNotNull("oilprice"), Restrictions.or(Restrictions.isNull("invalidationDate"),Restrictions.gt("invalidationDate", new Date())))); //日期为空或大于当下日期			        
			         List<ZynPara> listoil=c_oil.list();			        	
				        	if(listoil.size()>0){
				        		 session.put("oilprice", listoil.get(0).getOilprice());            
						        
				        	}else{//没有对应记录，返回0		        		
				        		session.put("oilprice", 0);  
				        	}                            
		        	
            //-----set the para end           
 
			//--company property------------------
		
				            TreeMap<String, String> mapcompropt=new TreeMap();
				            mapcompropt.put(Const.comTypetop,"总部");
				            mapcompropt.put(Const.comTypesubcom,"分公司");
				            mapcompropt.put(Const.comTypecustom,"客户");				            
				            mapcompropt.put(Const.comTypesupply,"供应商");
				            mapcompropt.put(Const.comTypethird,"第三方机构");
				            mapcompropt.put(Const.comTypeother,"其他");				         				            
				            session.put("map_com_prpt",mapcompropt);             	
				        	
			//--company property------------------    	
				            
			
			//--company property------------------
				    		
				            TreeMap<String, String> mapcartype=new TreeMap();
				            mapcartype.put(Const.carTypeA,"大");
				            mapcartype.put(Const.carTypeB,"中");
				            mapcartype.put(Const.carTypeC,"小");				            
				           	         				            
				            session.put("map_car_type",mapcartype);             	
				        	
			//--company property------------------    	
				            
            return SUCCESS;
        }else{
            this.addFieldError("name", "用户名有误!");
            return INPUT;
        }
    }
    
    public void validate() {
        //楠岃瘉鐮侀獙璇�
//        if(null == inputCertCode || "".equals(inputCertCode)){
//            this.addFieldError("inputCertCode", "楠岃瘉鐮佷笉鑳戒负绌�!");
//        }else{
//            Map session = ActionContext.getContext().getSession();
//            String certCode = session.get("certCode").toString();
//            if( ! certCode.equals(this.inputCertCode)){
//                this.addFieldError("inputCertCode", "楠岃瘉鐮佷笉姝ｇ‘!");
//            }
//        }
        
    }
    public String exitSys(){
    	b.getSession().clear();
    	b.closeSession();
    	return "login";
    }
    
    
}
